package com.such.kit.database.dialect.configs;

import java.util.HashMap;
import java.util.Map;
import com.such.kit.easyparam.ParamMap;
import com.such.kit.placeholder.PlaceholderCommons;

public class Sqlserver {

	private static Map<String, String> stringToDateMap = new HashMap<String, String>();
	static {
		stringToDateMap.put("yyyymmdd", "convert(datetime, #[field], 112)");
		stringToDateMap.put("yyyy-mm-dd", "convert(datetime, #[field], 23)");
		stringToDateMap.put("yyyymmddhh24miss", "convert(datetime, stuff(stuff(stuff(stuff(stuff(#[field], 5, 0, '-'), 8, 0, '-'), 11, 0, ' '), 14, 0, ':'), 17, 0, ':'), 20)");
		stringToDateMap.put("yyyy-mm-dd hh24:mi:ss", "convert(datetime, #[field], 20)");
		stringToDateMap.put("yyyymmddhh24missff3", "convert(datetime, stuff(stuff(stuff(stuff(stuff(stuff(#[field], 5, 0, '-'), 8, 0, '-'), 11, 0, ' '), 14, 0, ':'), 17, 0, ':'), 20, 0, '.'), 21)");
		stringToDateMap.put("yyyy-mm-dd hh24:mi:ss.ff3", "convert(datetime, #[field], 21)");
	}
	private static Map<String, String> dateToStringMap = new HashMap<String, String>();
	static {
		dateToStringMap.put("yyyymmdd", "convert(varchar(8), #[field], 112)");
		dateToStringMap.put("yyyy-mm-dd", "convert(varchar(10), #[field], 23)");
		dateToStringMap.put("yyyymmddhh24miss", "replace(replace(replace(convert(varchar(14), #[field], 20), '-', ''), ' ', ''), ':', '')");
		dateToStringMap.put("yyyy-mm-dd hh24:mi:ss", "convert(varchar(19), #[field], 20)");
		dateToStringMap.put("yyyymmddhh24missff3", "replace(replace(replace(replace(convert(varchar(23), #[field], 21), '-', ''), ' ', ''), ':', ''), '.', '')");
		dateToStringMap.put("yyyy-mm-dd hh24:mi:ss.ff3", "convert(varchar(23), #[field], 21)");
	}

	/**
	 * <p>方法名：stringToDate</p>
	 * <p>描述：字符串转换为日期</p>
	 * <p>逻辑：将 field 按 format 格式转换为日期</p>
	 * @param field 字段
	 * @param format 日期格式
	 * @return 函数
	 */
	public static String stringToDate(String field, String format) {
		return PlaceholderCommons.replacePlaceholder(stringToDateMap.get(format), new ParamMap<String, String>().putParam("field", field));
	}

	/**
	 * <p>方法名：dateToString</p>
	 * <p>描述：日期转换为字符串</p>
	 * <p>逻辑：将 field 按 format 格式转换为字符串</p>
	 * @param field 字段
	 * @param format 日期格式
	 * @return 函数
	 */
	public static String dateToString(String field, String format) {
		return PlaceholderCommons.replacePlaceholder(dateToStringMap.get(format), new ParamMap<String, String>().putParam("field", field));
	}

}

